package com.lyx.langchain4j.controller;


import com.lyx.langchain4j.ai.aiCodeHelperService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
@CrossOrigin
public class aiController {
    @Resource
    private aiCodeHelperService aiCodeHelperService;


    @GetMapping("/chat")
    public Flux<ServerSentEvent<String>> chat(int memoryId, String message){
        return aiCodeHelperService.chatStream(memoryId,message)
                .map(chunk -> ServerSentEvent.<String>builder()
                        .data(chunk)
                        .build());
    }
}
